/**
 * 抢单列表
 * @author levy
 * @author ciwyaitd 使用闭包管理变量
 * @date   16/1/7
 * @description [这个页面可以和sign_for.js重构为一个页面]
 */
import RushHeader from "../components/RushHeader";
import NoData from "../components/NoData";
import PullRefresh from "../components/mui/PullRefresh";
import {health} from "../const/offices";
import {prefix} from "../const/prefix";

liefeng.bridgeJs.bridgeReady(function( lf, r) {

    var ajax = lf.utils.ajax,
        api = health.api,
        apiUrl = prefix.oldpeople,
        doAction = lf.bridgeJs.doAction,
        dataId,
        appAlert = lf.bridgeJs.alert;

    var Rush = React.createClass({
        getInitialState:function(){
            return({
              visible:''
            });
        },
        rush:function(e){
            lf.utils.stopBubble(e);
            var dataAdd = this.props.data;
            ajax({
                url:apiUrl + api.doRush,
                type:'POST',
                data: {
                    wfTaskId: dataAdd.taskId,
                    staffid: dataId.id,
                },
                headers: {
                    openId: dataId.openId,
                },
                success: function (data) {
                    if(data.code == '200'){
                        mui.toast('抢单成功');
                        this.setState({
                            visible: 'ele-hide'
                        });
                        RushHeader.updateCount();
                    }else{
                        appAlert(data.desc, lf.appJs.doClose());
                    }
                }.bind(this)
            });
        },

        componentDidMount: function () {
            r.findDOMNode(this.refs.toDetail).addEventListener('tap', function (e) {

                var data = this.props.data;
                lf.bridgeJs.doAction('order-detail-oa.html?flag=rush&orderId='+data.orderId+"&taskId="+data.taskId,'抢单详情');
            }.bind(this));

            r.findDOMNode(this.refs.rush).addEventListener('tap', function (e) {
                this.rush(e);
            }.bind(this))

        },
        render:function(){
            var data = this.props.data;

            return (
                <li className={"mui-table-view-cell " + this.state.visible}>
                    <a  ref="toDetail" className="mui-navigate-right">
                        <span className="work-title">{data.title}</span>
                        <button ref="rush" className="rush-btn  lf-green-outlined-btn" >抢单</button>
                    </a>
                </li>
            );
        }
    });

    var RushList = React.createClass({
        getInitialState: function () {
            return ({
                noData:{
                    span_text:'本月还没有抢单信息',
                    btn_text:'查看工单处理',
                    page_url:'task-processing-oa.html',
                    page_title:'工单处理',
                },
            });
        },
        render: function () {
            var rushList = this.props.data.map(function(data){
                return <Rush data={data} />
            });
            if(this.props.total){
                return (
                    <ul className="mui-table-view">
                        {rushList}
                    </ul>
                );
            }
            else{
                return <NoData data={this.state.noData}/>;
            }



        }

    });

	var RushBody = React.createClass({
        getInitialState:function(){
            return ({
                header:{
                    text:'待抢单',
                    num:0,
                    monthlyProcessed:0
                },
                oaColor: 'oa-title',
                total:1,
                rushList:[],
            })
        },
        getHeader(user) {
            var me = this;
            ajax({
                url: apiUrl + api.listHeader,
                data:{actor: dataId.id},
                headers: {
                    openId: dataId.openId,
                },
                success: (data) => {
                    console.log(data)
                    data = data.data;
                    me.setState({
                        header:{
                            text:'待抢单',
                            num:data.grabCount || 0,
                            monthlyProcessed:data.monthCompleteCount || 0
                        },
                    })
                }
            });
        },
        getList(user) {
            var me = this;
            ajax({
                url: apiUrl + api.rushList,
                data:{
                    actor: dataId.id,
                    page:1,
                    size:999,
                },
                headers: {
                    openId: dataId.openId,
                },
                success: (data) => {
                    me.setState({
                        rushList:data.dataList,
                        total:data.dataList.length,
                    })
                }
            })
        },
        componentWillMount:function(){
            let user = this.props.user;
            this.getHeader(user);
            this.getList(user);

            lf.appJs.refresh(user => {
                this.getHeader(user);
                this.getList(user);
            });

        },
		render: function () {
            return (
                <div className="rush lf-list">
                    <RushHeader data={this.state.header} oaTitle={this.state.oaColor}/>
                    <RushList data={this.state.rushList} total={this.state.total}/>
                </div>
            );


		}
	});

    var Body = r.createClass({
        _conRender: function () {
            return (
                <RushBody  user={this.props.user}/>
            )
        },
        render: function () {
            return (
                <PullRefresh  conRender={this._conRender} />
            )
        }
    });

    lf.appJs.getUser(function (user) {
        ajax({
            url: apiUrl + api.getGlobalId,
            type: 'post',
            data: {
                custGlobalId: user.globalId || user.custLoginVo.globalId,//user.id,'C1606281657108af41b8e068927' ||
            },
            success: function (data) {
                dataId = data.data;
                r.render(
                    <Body  user={user}/>,
                    document.getElementById('root'),function(){
                    }
                )
            }
        })
    });

});
